/*
 * Copyright(c) 2018 优居科技 All rights reserved.
 * distributed with this file and available online at
 * http://www.yjyz.com/
 */
package com.zb.api.business.service.impl;


import com.zb.api.business.dto.PosPlanUpDto;
import com.zb.api.business.dto.PosWaresDto;
import com.zb.api.business.entity.PosPlanUpEntity;
import com.zb.api.business.mapper.PosPlanMapper;
import com.zb.api.business.mapper.PosWaresMapper;
import com.zb.api.business.service.IPosWaresService;
import com.zb.api.common.models.Page;
import org.springframework.beans.BeanUtils;
import com.zb.api.business.entity.PosWaresEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
* 材料表
 *
 * @author cwp
 * @version v1.0
 * @CreationTime: - 2022-02-08 14:19:31
 * @Description:
 */
@Service
public class PosWaresServiceImpl implements IPosWaresService {

    @Autowired
    private PosWaresMapper posWaresMapper;

    @Autowired
    private PosPlanMapper posPlanMapper;

    @Override
    public Page<PosWaresDto> getPosWaresList(int pageNo, int pageSize, PosWaresDto posWaresDto){
        /*PageHelper.startPage(pageNo, pageSize);
        PosWaresEntity posWaresEntity = BeanUtils.copyProperties(posWaresDto, PosWaresEntity.class);
        return BeansUtils.toPage(posWaresMapper.getList(posWaresEntity), PosWaresDto.class);*/
        return null;
    }

    @Override
    public PosWaresDto get(String id) {
        PosWaresDto posWaresDto = new PosWaresDto();
        BeanUtils.copyProperties(posWaresMapper.get(id), posWaresDto);
        return posWaresDto;
    }

    @Override
    public int update(PosWaresDto posWares){

        return posWaresMapper.update(posWares);
    }

    @Override
    public int insert(PosWaresDto posWares){
        posWares.setCreateTm(new Date().getTime());
        return posWaresMapper.insert(posWares);
    }

    @Override
    public int delete(PosWaresDto posWares){

        return posWaresMapper.delete(posWares);
    }

    @Override
    public List<PosWaresDto> getWaresList(PosWaresDto posWaresDto) {
        return posWaresMapper.getList(posWaresDto);
    }

    @Override
    public Integer delPosWares(PosWaresDto posWaresDto) throws IllegalAccessException {
        PosPlanUpDto posPlanUpDto = new PosPlanUpDto();
        posPlanUpDto.setMerchantId(posWaresDto.getMerchantId());
        posPlanUpDto.setWaresCodes(posWaresDto.getId()+"");
        List<PosPlanUpDto> list = posPlanMapper.getUpList(posPlanUpDto);
        if (!CollectionUtils.isEmpty(list)){
            throw new IllegalAccessException("当前材料存在上架的方案中，不能移除操作");
        }

        posWaresMapper.delete(posWaresDto);
        return null;
    }

    @Override
    public Page<PosWaresDto> getList(PosWaresDto posWaresDto) {
        Page<PosWaresDto> page = new Page<>();
        List<PosWaresDto> list = posWaresMapper.getList(posWaresDto);
        page.setList(list);
        page.setPageNo(posWaresDto.getPageNo());
        page.setRowCntPerPage(posWaresDto.getPageSize());
        return page;
    }
}
